<?php

declare(strict_types=1);

namespace App\Wxc\Controller\SharerMp\V1;

use App\Wxc\Request\SharerMp\V1\GetShopDetailRequest;
use App\Wxc\Request\SharerMp\V1\GetShopListRequest;
use App\Wxc\Service\Input\SharerMpListRecShopInput;
use App\Wxc\Service\Query\ShopInfoQuery;
use App\Wxc\Service\WxcSharerMpShopRecService;
use App\Wxc\Service\WxcShopInfoService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Mine\Middlewares\CheckModuleMiddleware;
use Mine\MineController;
use Psr\Http\Message\ResponseInterface;
use Throwable;

/**
 * 店铺接口
 */
#[Controller(prefix: "wxc/sharer-mp/v1/shop")]
#[Middleware(middleware: CheckModuleMiddleware::class)]
class ShopController extends MineController
{
    #[Inject]
    protected WxcShopInfoService $shopInfoService;

    /**
     * 获取店铺详情
     */
    #[GetMapping("detail")]
    public function getDetail(): ResponseInterface
    {
        $req = GetShopDetailRequest::validateAndCreate($this->request->all());
        return $this->success('ok', $this->shopInfoService->detail($req->shop_id));
    }

    /**
     * 获取推荐店铺列表
     * @throws Throwable
     */
    #[GetMapping("rec-list")]
    public function getRecList(): ResponseInterface
    {
        $input = SharerMpListRecShopInput::validateAndCreate($this->request->all());
        $output = container()->get(WxcSharerMpShopRecService::class)->getShopList($input);
        return $this->success('ok', $output);
    }

    /**
     * 获取店铺列表
     */
    #[GetMapping("list")]
    public function getList(): ResponseInterface
    {
        $req = GetShopListRequest::validateAndCreate($this->request->all());
        $getList = ShopInfoQuery::from($req);
        if ($req->keywords) {
            $getList->search([
                'nickname' => $req->keywords,
            ]);
        }

        $result = $this->shopInfoService->queryCursorPagination('shop_id', $getList, $req->next_key, $req->page_size);

        foreach ($result['list'] as $i => $item) {
            $result['list'][$i] = $this->shopInfoService->detail($item);
        }

        // todo delete 复制一些list元素，用来测试
        for ($i = 1; $i < 10; $i++) {
            $result['list'][] = $result['list'][0];
        }

        return $this->success('ok', $result);
    }
}
